import { THREE } from '@amcax/web-render';
import { KeepSizeObject3D } from './KeepSizeObject3D';
type CSArrrowHelperAttrs = {
  startPoint: THREE.Vector3;
  direction: THREE.Vector3;
  shaftLength?: number; // 箭杆的长度
  headLength?: number; // 箭头长度
  shaftRadius?: number; // 箭杆宽度
  headRadius?: number; // 箭头宽度
  color?: number; // 颜色
  camera?: THREE.OrthographicCamera | THREE.PerspectiveCamera;
};
export default class CSArrrowHelper extends THREE.Group {
  // 起始点
  startPoint: THREE.Vector3;
  direction: THREE.Vector3;
  keepSizeObject3D: KeepSizeObject3D;
  camera: THREE.OrthographicCamera | THREE.PerspectiveCamera;
  // 箭杆的长度
  shaftLength: number = 1;
  // 箭头长度
  headLength: number = 0.2;
  // 箭杆宽度
  shaftRadius: number = 0.5;
  headRadius: number = 0.5;
  color: number = 0xffff00;
  scene: THREE.Group;
  constructor(attrs: CSArrrowHelperAttrs) {
    super();
    this.keepSizeObject3D = new KeepSizeObject3D(attrs.camera);
    this.setAttrs(attrs);
    this.createArrow();
  }
  setAttrs(attrs: CSArrrowHelperAttrs) {
    this.startPoint = attrs.startPoint;
    this.direction = attrs.direction;
    this.shaftLength = attrs.shaftLength || 1; // 箭杆的长度
    this.headLength = attrs.headLength || 0.2; // 箭头长度
    this.shaftRadius = attrs.shaftRadius || 0.5; // 箭杆宽度
    this.headRadius = attrs.headRadius || 0.5; // 箭头宽度
    this.color = attrs.color || 0xffff00; // 颜色
    this.camera = attrs.camera || this.camera; // 相机;
  }
  private createArrow() {
    const shaftGeometry = new THREE.CylinderGeometry(
      this.shaftRadius,
      this.shaftRadius,
      this.shaftLength,
      16,
    );
    shaftGeometry.translate(0, this.shaftLength / 2, 0);
    const shaft = new THREE.Mesh(
      shaftGeometry,
      new THREE.MeshBasicMaterial({ color: this.color }),
    );
    const headGeometry = new THREE.ConeGeometry(
      this.headRadius,
      this.headLength,
      16,
    );
    headGeometry.translate(0, this.shaftLength + this.headLength / 2, 0);
    const head = new THREE.Mesh(
      headGeometry,
      new THREE.MeshBasicMaterial({ color: this.color }),
    );
    this.keepSizeObject3D.add(shaft);
    this.keepSizeObject3D.add(head);
    this.add(this.keepSizeObject3D);
    this.position.copy(this.startPoint);
    const yAxis = new THREE.Vector3(0, 1, 0);
    const axis = yAxis.clone().cross(this.direction);
    const angle = yAxis.angleTo(this.direction);

    // 处理方向与Y轴平行的情况
    if (axis.length() < 0.0001) {
      if (this.direction.y > 0) {
        this.quaternion.identity(); // 无需旋转（与Y轴同向）
      } else {
        this.quaternion.setFromAxisAngle(new THREE.Vector3(1, 0, 0), Math.PI); // 绕X轴旋转180°
      }
    } else {
      this.quaternion.setFromAxisAngle(axis.normalize(), angle);
    }
  }
  destroy() {
    this.scene?.remove(this);
    this.keepSizeObject3D.children.forEach((child) => {
      if (child instanceof THREE.Mesh) {
        child.geometry?.dispose();
        child.material?.dispose();
      }
    });
    this.keepSizeObject3D = null;

    this.children.forEach((child) => {
      if (child instanceof THREE.Mesh) {
        child.geometry?.dispose();
        child.material?.dispose();
      }
    });
    this.startPoint = null;
    this.direction = null;
  }
  /**
   * @description 添加到场景中去
   * @param { Object3D } scene
   **/
  addTo(scene: THREE.Group) {
    this.scene = scene;
    this.scene.add(this);
  }
}
